/*
----------------------------------------------------------------------------
Copyright (c) 2007 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*------------------------- BasalRecorder.hpp -----------------------------*/
/*===========================================================================
FILE: BasalRecorder.hpp
Author: Wangxing SHI (swxlion)
Version: 0.0.1
Date: 2009-06-06

File Description:
    Define the base class and interface for Recorder.

Dependencies:
    Platform: Platform Independent.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       09-06-06   Created the first version.
===========================================================================*/
#ifndef _LUNA_LOG__BASAL_RECORDER_H_
#define _LUNA_LOG__BASAL_RECORDER_H_

#include "Constant.hpp"
#include "TMemoryPoolCore.hpp"
#include "GDST/TQueue.h"
/*===============================================================================
PROTOTYPES DECLARATION
=============================================================================== */
namespace LunaLog
{
	namespace Helper
	{
		/*========================================
		  Recorder Data Section
		==========================================*/
		struct	RecorderDataSection
		{
			unsigned char			buf[LunaLog::Constant::stMaxLogDataSectionLength];
			RecorderDataSection		*NextSection;
		};
	}

	/*========================================
	  Basal Recorder
	==========================================*/
	class BasalRecorder
	{
		typedef		ASDL::TDLinkNode<Helper::RecorderDataSection>		RecorderMemoryNode;

	public:
		enum BOMType{ UTF8, USC2, Unicode, None };
		enum WorkLevelType { Selector, Filter };

	private:
		ASDL::TDLinkNode<BasalRecorder *> *			pRecorderList;
		ASDL::TQueue<Helper::RecorderDataSection>	DataSectionQueue;		

		LunaLog::TMemoryPoolCore<RecorderMemoryNode>	clsRMNMP;	//-- Recorder Memory Node Memory Pool.
		
#ifdef _MSC_VER
		CRITICAL_SECTION		Critical;
		HANDLE					hSemaphore;
#else
		pthread_mutex_t			Mutex;
		pthread_cond_t			Condition;
#endif

		int				iWorkLevel;
		WorkLevelType	enWorkType;
		bool			bIsInited;
		bool			bIsFrozen;
		bool			bIsSyncMode;
		bool			bIsStringMode;
		
#ifdef _MSC_VER
		static DWORD WINAPI			RecorderDaemon(LPVOID lpParameter);
#else
		static void *				RecorderDaemon(LPVOID lpParameter);
#endif

	protected:
		virtual int RealRecord(const void * data, int len) = 0;

		bool			InitCritical();
		URESULT			InnerSyncInit();
		URESULT			InnerAsyncInit( bool StringMode = false,
										ULONG ulInitNumber = LunaLog::Constant::MemoryPool::gc_ulInitNum,
										ULONG ulMaxNumber = LunaLog::Constant::MemoryPool::gc_ulMaxNum,
										ULONG ulPerIncNumber = LunaLog::Constant::MemoryPool::gc_ulIncNum,
										ULONG ulPerfactNumber = LunaLog::Constant::MemoryPool::gc_ulPerfectNum,
										bool bBlock = false );
		URESULT			InnerInit(  bool bIsSync,
									bool StringMode = false,
									ULONG ulInitNumber = LunaLog::Constant::MemoryPool::gc_ulInitNum,
									ULONG ulMaxNumber = LunaLog::Constant::MemoryPool::gc_ulMaxNum,
									ULONG ulPerIncNumber = LunaLog::Constant::MemoryPool::gc_ulIncNum,
									ULONG ulPerfactNumber = LunaLog::Constant::MemoryPool::gc_ulPerfectNum,
									bool bBlock = false );
		URESULT			InnerFree();

	public:
		void			Freeze();

	public:
		BasalRecorder();
		virtual ~BasalRecorder();

		void Append(BasalRecorder *);
		void Append(BasalRecorder &);
		void Remove(BasalRecorder *);
		void Remove(BasalRecorder &);

	public:
		int				Record(const void* data, int len);
		int				Record(const void* data, int len, int level);

		bool			IsInited();
		bool			IsSyncMode();
		bool			IsAsyncMode();
		//--- [Initialization functions] ---------------------------
		void			SetBOMType(BOMType type);
		BOMType			GetBOMType();
		void			SetWorkLevel(int level);
		int				GetWorkLevel();
		void			SetWorkLevelType(WorkLevelType type);
		WorkLevelType	GetWorkLevelType();
		void			SetDisabledLevel(int level);
		void			SetSelectedLevel(int level);

		bool			RegisterToRecorderPool(const char * szRecorderName);

		bool			GetMemoryPoolStatus( MemoryPoolStatus &strMPS );
		bool			GetMemoryPoolStatusSnapshot( MemoryPoolStatus &strMPS );

	};
}

#endif // _LUNA_LOG__BASAL_RECORDER_H_
